﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;

namespace Department.admin.Content
{
    public partial class ViewAllDynamicTypes : System.Web.UI.Page
    {
        bool wasSortedDesc = AdminVariablesSingleton.WasSortedDesc;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.IsAuthenticated)
            {
                Response.Redirect("/admin/Login.aspx");
            }
            this.Page.Title = "Всички видове динамично съдържание";

            AdminVariablesSingleton.changeCSS(Master ,"dynStuff");

            Control foundControl = AdminVariablesSingleton.FindControlRecursive(Master, "viewDynTypes");
            ((HtmlControl)foundControl).Attributes.Add("class", "sub_show");
        }

        
        protected void sortByHeading_Click(object sender, CommandEventArgs e)
        {
            string item = e.CommandArgument.ToString();

            string sqlText;
            if (wasSortedDesc)
            {
                sqlText = itemsViewDataSource.SelectCommand + " ORDER BY " + item + " DESC";
                AdminVariablesSingleton.WasSortedDesc = false;
            }
            else
            {
                sqlText = itemsViewDataSource.SelectCommand + " ORDER BY " + item;
                AdminVariablesSingleton.WasSortedDesc = true;
            }

            itemsViewDataSource.SelectCommand = sqlText;
            itemsView.DataBind();

        }

        protected void toggleall_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkHeader = (CheckBox)sender;
            for (int i = 0; i < itemsView.Items.Count; i++)
            {
                CheckBox chk = (CheckBox)itemsView.Items[i].FindControl("CheckBoxIsMarked");
                chk.Checked = chkHeader.Checked;
            }
        }


        protected void itemsView_ItemCommand(object sender, ListViewCommandEventArgs e)
        {          
            if (e.CommandName == "Save")
            {
                TextBox tb = (TextBox)e.Item.FindControl("txtName");
              
                string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DepartmentConnectionString"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(strConnString))
                {
                    string Sql = "INSERT INTO [Type] ([Name]) VALUES (@Name)";
                    try
                    {
                        conn.Open();

                        using (SqlCommand dCmd = new SqlCommand(Sql, conn))
                        {
                            dCmd.Parameters.AddWithValue("@Name", tb.Text.Trim());

                            dCmd.ExecuteNonQuery();
                        }
                    }
                    catch (SqlException sqle)
                    {
                        Response.Redirect("/admin/ErrorPage.aspx?type=sql&err=" + sqle.Message);
                    }
                    catch (Exception ex)
                    {
                        Response.Redirect("/admin/ErrorPage.aspx?type=1&err=" + ex.Message);
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
                itemsView.DataBind();
            }
        }
    }
}